package com.example.demo.service;

import com.example.demo.dao.customerMapper;
import com.example.demo.pojo.Customer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class CustomerService {
    @Autowired
    customerMapper customermapper;

    @Transactional(timeout = 10, readOnly = true, rollbackFor = Exception.class)
    public Customer selectCustomerByid(int id){
        return customermapper.selectCustomerByid(id);
    }

    @Transactional(timeout = 10, readOnly = true, rollbackFor = Exception.class)
    public List<Customer> selectAllCustomers(){
        return customermapper.selectAllCustomers();
    }

    @Transactional(timeout = 10, rollbackFor = Exception.class)
    public void addCustomer(Customer customer){
        customermapper.addCustomer(customer);
    }

    @Transactional(timeout = 10, rollbackFor = Exception.class)
    public void updateCustomer(Customer customer){
        customermapper.updateCustomer(customer);
    }

    @Transactional(timeout = 10, rollbackFor = Exception.class)
    public void deleteCustomer(int id){
        customermapper.deleteCustomer(id);
    }

    @Transactional(timeout = 10, rollbackFor = Exception.class)
    public void changeAll(List<Customer> customerlist){
        for(Customer customer : customerlist){
            customermapper.updateCustomer(customer);
        }
    }

    @Transactional(timeout = 10, readOnly = true, rollbackFor = Exception.class)
    public int cust_num(){
        return customermapper.cust_num();
    }
}

